package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.constant.WareTaskConstant;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: xu
 * @Date: 2022/06/20/11:14
 * @Description: 702来料检验申请单======>>>>71采购订单
 */
@Component
public class Checkout implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;

    @Override
    public ErpType gainType() {
        return new ErpType(702, 1);
    }


    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {
        //过滤 VMI 跟直发虚仓
        List<AsnDetail> newAsnDetail =
                asnDetails.stream().filter(
                        item -> ObjectUtil.isEmpty(item.getErpStockId())
                                || !pushErpService.getNotAasnStock().contains(item.getErpStockId())).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(newAsnDetail)) {
            return null;
        }

        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        for (AsnDetail asnDetail : asnDetails) {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(asnDetail.getSourceId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getRowIndex()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty());//数量
//            orderDetailBO.setAuxPropId(33469);

            orderDetailBO.setStockId(asnDetail.getStockId());//仓库部
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);


//        throw  new ServiceException()
//        throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未匹配到货位标签模板");

        return pushErpService.pushIMBIll(orderToErpBO);

    }
}
